﻿/*
Author: MBH
Date Authored: 8/13/09
*/

//Module that adds delete buttons to the grid.
function RowDeleteModule() {
	var base = this;

	//Adds a column to hold the delete button, then registers for rowBound events
	//so that the button can be added.
	base.initialize = function(liteGrid, options) {

		options.columns.push({ field: "RowDeleteModule", header: "&nbsp;" });

		liteGrid.$el.bind("rowBound", base.rowBound);

		//After initialization, we can grab the final index of the row delete column
		liteGrid.$el.bind("initializationComplete", function() {

			$(options.columns).each(function(i) {
				if (this.field == "RowDeleteModule") base.columnIndex = i;
			});

		});

		//If the tree grid module is loaded, we have to perform deletes recursively.
		var treeGrid = liteGrid.getModule("TreeGridModule");
		if (treeGrid != null) {
			base.treeGridEnabled = true;
		}

		//Grab references for fast access later
		base.options = options;
		base.liteGrid = liteGrid;
		base.dataProvider = liteGrid.dataProvider;
	}

	//Adds the delete button in to every row. 
	base.rowBound = function(event, row, dataItem) {
		//Grab the cell to add the button to
		var cell = $("td:eq(" + base.columnIndex + ")", row);

		var button = $("<div class='ui-icon ui-icon-circle-close delete-button'></div>");
		button.click(base.deleteClicked);

		cell.append(button);
	}

	//Callback that is fired when a delete button is clicked.  This function
	//actually performs the delete.
	base.deleteClicked = function() {
	
		var button = $(this);

		//Get the row that was clicked and the data item's ID
		var row = button.parents(":eq(1)");
		var dataItem = row.data("dataItem");
		var id = dataItem[base.options.rowIdColumn];

		// notify that a delete is going down
		var beforeDeleteEvent = $.Event("beforeDelete");
		beforeDeleteEvent.row = row;
		beforeDeleteEvent.dataItem = dataItem;
		beforeDeleteEvent.id = id;
		base.liteGrid.$el.trigger(beforeDeleteEvent);
		// check for any cancellations...
		if (beforeDeleteEvent.cancel) {
			var event = $.Event("actionCancelled");
			event.message = "Delete cancelled: " + beforeDeleteEvent.cancelMessage;
			base.liteGrid.$el.trigger(event);
			return false;
		}

		//Mark the row as deleted.
		row.addClass("deleted");
		dataItem[base.options.deleteColumnName] = true;

		//Hide the row and all it's children. 
		var toHide = row;

		if (base.treeGridEnabled) {
			//First, undo the deletes of any child rows.  They'll be deleted when the parent is deleted.
			base.liteGrid.getChildren(id).find(".restore-button").click();
			toHide = toHide.add(base.liteGrid.getChildren(id));
		}

		var disabledClones = toHide.clone();

		for (var i = 0; i < disabledClones.length; i++) {
			disabledClones.eq(i).data("dataItem", toHide.eq(i).data("dataItem"));
		}
		
		disabledClones.insertBefore(row).addClass("pending-delete");
		toHide.remove();

		//Remove the delete button from the deleted row, and add a restore button.
		disabledClones.eq(0)
			//Add selector to the row which is used to find rows that should be deleted/
			//restored when the delete is processed.
			.data("rowsToDelete", disabledClones)
			//Find the rows delete button and make it a restore button.
			.find("div.ui-icon-circle-close")
			.removeClass("ui-icon-circle-close delete-button")
			.addClass("ui-icon-arrowreturnthick-1-w restore-button")
			.click(base.restoreClicked);
	}

	//Restores a set of rows that had previously been deleted.
	base.restoreClicked = function() {

		var row = $(this).parents(":eq(1)");

		var rowsToDelete = row.data("rowsToDelete");
		row.removeData("rowsToDelete");

		//Re-bind the rows, this makes the clones "live" and reattaches all event-handlers.
		rowsToDelete.each(function() {
			var row = $(this);
			base.liteGrid.bindRow(row, row.data("dataItem"));
		});

		//Restore their CSS to the original, non-deleted state.
		rowsToDelete.removeClass("pending-delete");

		//Undelete the row.
		row.removeClass("deleted");
		delete row.data("dataItem")[base.options.deleteColumnName];
	}
}